package leetcode.f1t100;

import java.util.ArrayList;
import java.util.List;

/**
 * 给一个不重复集合, 返回任意顺序的所有唯一子集
 *
 * https://leetcode.com/problems/subsets/
 *
 * @author lichx
 * @createTime 2024/1/26 10:43
 */
public class Q78_Subsets {

    public List<List<Integer>> subsets(int[] nums) {
        ArrayList<List<Integer>> answer = new ArrayList<>();
        subSets(answer, nums, 0);
        return answer;
    }

    public void subSets(List<List<Integer>> answer, int[] nums, int index) {
        if(index == nums.length) {
            answer.add(new ArrayList<>());
            return;
        }
        subSets(answer, nums, index +1);
        int size = answer.size();
        for (int i = 0; i < size; i++) {
            List<Integer> list = answer.get(i);
            answer.add(new ArrayList<>(list));
            list.add(nums[index]);
        }
    }

    public static void main(String[] args) {
        Q78_Subsets bean = new Q78_Subsets();
        int[] nums = {1, 2, 3};
        List<List<Integer>> subsets = bean.subsets(nums);
        for (List<Integer> list : subsets) {
            for (Integer i : list) {
                System.out.print(i+" ");
            }
            System.out.println();
        }
    }
}
